package mysql

import "gorm.io/gorm"

type Order struct {
	gorm.Model
	UserId         int     `gorm:"type:varchar(11);not null;comment:用户Id"`
	PurchaserId    int     `gorm:"type:varchar(11);not null;comment:购票人Id"`
	PurchaserName  string  `gorm:"type:varchar(50);not null;comment:姓名"`
	OrderNumber    string  `gorm:"type:varchar(100);not null;comment:订单号"`
	ScenicSpotId   int     `gorm:"type:varchar(11);not null;comment:景区地区Id"`
	ScenicSpotName string  `gorm:"type:varchar(100);not null;comment:景区名"`
	Fare           float64 `gorm:"type:decimal(10,2);not null;comment:票价"`
	Num            int     `gorm:"type:varchar(11);not null;comment:数量"`
	Insure         int     `gorm:"type:varchar(11);default:1;comment:是否投保 1.没有 2.有"`
	OrderStatus    int     `gorm:"type:varchar(11);default:3;not null;comment:订单状态 1.已完成 2.已支付 3.待支付 4.未支付(交易异常)"`
}

func (o Order) CreateOrder() error {
	return DB.Create(&o).Error
}

func (o Order) ShowOrderByOrderSn(sn string) (result *Order, err error) {
	err = DB.Model(&o).Where("order_number = ?", sn).Limit(1).Find(&result).Error
	return
}

func (o Order) UpdateOrderStatus(sn string, status string) error {
	return DB.Model(&o).Where("order_number = ?", sn).Update("order_status", status).Error
}

func (o Order) ShowOrderList(id int64) (result []*Order, err error) {
	err = DB.Model(&o).Where("id = ?", id).Find(&result).Error
	return
}
